<script setup>
import {
  getRole,
  batchSaveRole,
  updateRole,
  checkRoleNameUnique,
  checkRoleKeyUnique,
  findMaxSort
} from "@/api/system/role";
import { randomUUID } from "@/utils/util";
const props = defineProps({
  id: {
    type: [Number, String],
    default: ""
  }
});

const formRef = ref();
const data = ref({
  loading: false,
  form: {
    id: props.id
  },
  rules: {
    roleName: [
      { required: true, message: "请输入角色名称", trigger: "blur" },
      { validator: getcheckRoleNameUnique }
    ],
    roleKey: [
      { required: true, message: "请输入角色编码", trigger: "blur" },
      { validator: getcheckRoleKeyUnique }
    ]
  }
});

onMounted(() => {
  if (data.value.form.id != "") {
    getInfo();
  } else {
    findMaxSort().then((res) => {
      data.value.form.sort = res.data;
    });
  }
});

function getInfo() {
  data.value.loading = true;
  getRole(props.id).then((res) => {
    data.value.loading = false;
    data.value.form = res.data;
  });
}

function getcheckRoleNameUnique(rule, value, callback) {
  if (data.value.form.id !== "" || data.value.form.id !== undefined) {
    callback();
  }
  const msg = "角色名称已存在";
  if (value === "") {
    callback();
  } else {
    const checkData = {
      roleName: value
    };
    checkRoleNameUnique(checkData).then((response) => {
      if (response.data.code === "1") {
        callback();
      } else {
        callback(msg);
      }
    });
  }
}
function getcheckRoleKeyUnique(rule, value, callback) {
  if (data.value.form.id !== "" || data.value.form.id !== undefined) {
    callback();
  }
  const msg = "角色编码已存在";
  if (value === "") {
    callback();
  } else {
    const checkData = {
      roleKey: value
    };
    checkRoleKeyUnique(checkData).then((response) => {
      if (response.data.code === "1") {
        callback();
      } else {
        callback(msg);
      }
    });
  }
}

// 如果父组建需要调用子方法，用defineExpose 导出该方法。
defineExpose({
  submit(callback) {
    formRef.value.validate((valid) => {
      const datas = new Array();
      datas.push(data.value.form);
      if (data.value.form.id == "" || data.value.form.id == undefined) {
        const id = randomUUID();
        data.value.form.id = "newRow-" + id;
        data.value.form.handleType = "add";
      } else {
        data.value.form.handleType = "edit";
      }
      batchSaveRole(datas).then(() => {
        ElMessage.success({
          message: "操作成功",
          center: true
        });
        callback && callback();
      });
      // if (data.value.form.id !== "" || data.value.form.id !== undefined) {
      //   batchSaveRole(data.value.form).then(() => {
      //     ElMessage.success({
      //       message: "操作成功",
      //       center: true
      //     });
      //     callback && callback();
      //   });
      // } else {
      //   batchSaveRole(data.value.form).then(() => {
      //     ElMessage.success({
      //       message: "保存成功",
      //       center: true
      //     });
      //     callback && callback();
      //   });
      // }
    });
  }
});
</script>

<template>
  <div v-loading="data.loading">
    <el-form
      ref="formRef"
      :model="data.form"
      :rules="data.rules"
      label-width="120px"
      label-suffix=" "
      label-position="top"
      style="padding: 0 20px"
    >
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="角色名称" prop="roleName">
            <el-input
              placeholder="请输入角色名称"
              v-model="data.form.roleName"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="角色编码" prop="roleKey">
            <el-input
              placeholder="请输入角色编码"
              v-model="data.form.roleKey"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12"
          ><el-form-item label="状态" prop="status">
            <el-radio-group v-model="data.form.status">
              <el-radio-button label="0">正常</el-radio-button>
              <el-radio-button label="1">禁止</el-radio-button>
            </el-radio-group>
          </el-form-item></el-col
        >
        <el-col :span="12">
          <el-form-item label="排序" prop="sort">
            <el-input placeholder="" v-model="data.form.sort" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>
